<template>
  <div class="login-container">
    <!--      表单结构-->
    <el-form ref="myform" :model="form"  :rules="rules" class="myform">
      <h3>
        <img alt="" src="~@/assets/common/login-logo.png">
      </h3>
      <!--      用户手机号-->
      <el-form-item prop="mobile">
        <el-input
          v-model="form.mobile"
          placeholder="请输入用户名"
          prefix-icon="el-icon-user-solid"
        />
      </el-form-item>
      <!--      密码-->
      <el-form-item prop="password">
        <el-input
          show-password
          v-model="form.password"
          placeholder="请输入密码"
          prefix-icon="el-icon-lock"
        />
      </el-form-item>
      <!--      登录按钮-->
      <el-form-item>
        <el-button :loading="loading" type="primary" @click="login" class="login-btn">登录</el-button>
      </el-form-item>

      <div class="register">
        <a href="javascript:;">还没有账号？立即注册</a>
      </div>

    </el-form>
  </div>
</template>

<script>
//引入vuex的辅助函数
import {mapActions} from 'vuex'
//引入校验规则方法
import {validateMobile} from "@/utils/validate";

export default {
  name: 'Login',
  data() {
    return {
      loading:false,//登录加载状态
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      //校验规则
      rules: {
        mobile: [
          {required: true, message: '请输入手机号码', trigger: 'blur'},
          //自定义校验规则
          {validator: validateMobile, trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 16, message: '密码长度需为6-16位', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    ...mapActions({userLogin: "user/login"}),
    login() {
      //完成表单校验
      this.$refs.myform.validate( async res => {
        // console.log(res);
        if (res) {
          try {
            //显示加载状态
            this.loading = true
            //校验通过发送请求
            await this.userLogin(this.form)
            //提示登录成功
            this.$message.success('登录成功')

            // 判断是否直接访问login页面还是别的页面由于未登录跳转过来的
            if(this.$route.query.toPath) {
              this.$router.replace(this.$route.query.toPath)
            } else{
            this.$router.replace('/')
            }
          }catch(error){
            console.log(error)
          }finally{
            //不论成功失败都执行  关闭加载状态
            this.loading = false
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
.login-container {
  height: 100%;
  background-image: url("~@/assets/common/login.jpg");
  background-size: 100% 100%;
  h3 {
    img {
      margin-bottom: 50px
    }
  }

  .myform {
    width: 520px;
    padding: 0 35px;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
   ::v-deep .el-input__inner{
      height: 50px;
      padding-left: 40px;
      color: #68b0fe;
      background: hsla(0,0,100%,.7);
    }
    .login-btn{
      width: 100%;
      height: 64px;
      background: #407ffe;
      font-size: 24px;
      color: #fff;
    }
    .register{
      margin-top: 30px;
      color: #fff;
      text-align: center;
      font-size: 16px;
    }
  }
}
</style>
